// services/cabinetbundle/fetchCabinetBundle.js
const config = require('../../utils/config');
import { apiRequest } from '../_utils/apiRequest';

async function fetchCabinetBundle() {
  try {
    const cabinetData = await apiRequest({
      url: `${config.backendApiUrl}/cabinetBundles`,
      method: 'GET',
    });
    
    // 拼接图片 URL
    if (Array.isArray(cabinetData)) {
      cabinetData.forEach((item) => {
        let imgUrl = item.imageUrl;
        // 若后端没返回或返回空字符串，需要先校验
        if (!imgUrl) {
          return; // 或者可给个默认图
        }
        // 若不是 '/' 开头，就补上 '/'
        if (!imgUrl.startsWith('/')) {
          imgUrl = '/' + imgUrl;
        }
        item.imageUrl = `${config.backendUrl}${imgUrl}`;
      });
    }

    return cabinetData;
  } catch (error) {
    console.error('获取酒窖详情失败', error);
    return null; 
  }
}


module.exports = {
    fetchCabinetBundle
};
